package com.example.zg.zg_312_ordering_new.activity;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.beans.User;
import com.example.zg.zg_312_ordering_new.beans.UserinfoBean;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.example.zg.zg_312_ordering_new.listener.FoodOrderListener;
import com.example.zg.zg_312_ordering_new.listener.RetrofitListener;
import com.example.zg.zg_312_ordering_new.listener.UserinfoListener;
import com.example.zg.zg_312_ordering_new.model.FoodOrderModel;
import com.example.zg.zg_312_ordering_new.model.RetrofitBaseModel;
import com.example.zg.zg_312_ordering_new.model.ShopModel;
import com.example.zg.zg_312_ordering_new.model.UserModel;
import com.example.zg.zg_312_ordering_new.model.UserinfoModel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class PurchaseActivity extends AppCompatActivity {
    TextView tv_username,tv_shopname,tv_pirce,tv_foodname,tv_date,tv_sum;
    EditText et_address,et_time,et_num;
    Button bt_buy;
    ImageView iv_add,iv_reduce,iv_return;
    private String shopname,foodname,suggesttime,address,username;
    private  int food_id,user_id,num;
    private double sum,price;
    private SharedPreferences sharedPreferences;

    SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date curDate=new Date(System.currentTimeMillis());
    String date=formatter.format(curDate);    //获取当前系统时间


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);


        initData();
        initView();


        num=Integer.parseInt(et_num.getText().toString());
        suggesttime=et_time.getText().toString();
        address=et_address.getText().toString();

//        UserinfoModel userinfoModel=new UserinfoModel();
//        userinfoModel.getUserById(user_id,userinfoListener);

        click();
    }

    public void  initView(){
        tv_username=findViewById(R.id.textView40);
        tv_pirce=findViewById(R.id.textView49);
        tv_foodname=findViewById(R.id.textView45);
        tv_date=findViewById(R.id.textView51);
        tv_sum=findViewById(R.id.textView53);

        et_address=findViewById(R.id.editText9);
        et_time=findViewById(R.id.editText13);
        et_num=findViewById(R.id.editText14);

        bt_buy=findViewById(R.id.button10);

        iv_add=findViewById(R.id.imageView48);
        iv_reduce=findViewById(R.id.imageView47);
        iv_return=findViewById(R.id.imageView37);

        tv_foodname.setText(foodname);
        tv_pirce.setText("￥"+price);
        tv_date.setText(date);
        tv_sum.setText("￥"+price);
        tv_username.setText(username);
    }

    public void initData(){
        shopname=getIntent().getStringExtra("shopname");
        foodname=getIntent().getStringExtra("foodname");
        price=getIntent().getDoubleExtra("price",0.0);
        food_id=getIntent().getIntExtra("foodid",0);

        sharedPreferences=getSharedPreferences("userInfo",MODE_PRIVATE);
        user_id=sharedPreferences.getInt("user_id",0);
        username=sharedPreferences.getString("username","fail");
    }

    public void click(){
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num++;
                et_num.setText(""+num);
                tv_sum.setText("￥"+(num*price));
            }
        });
        iv_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num--;
                if (num<1){
                    num=1;
                }
                et_num.setText(""+num);
                tv_sum.setText("￥"+(num*price));
            }
        });

        bt_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodOrderModel foodOrderModel=new FoodOrderModel();
                foodOrderModel.insertOrder(user_id,food_id,num,num*price,suggesttime,address,foodOrderListener);
            }
        });

        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    FoodOrderListener foodOrderListener=new FoodOrderListener() {
        @Override
        public void onResponse(User user) {
            if (user.getSuccess().equals("1")){
                Toast.makeText(PurchaseActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                finish();
            }else{
                Toast.makeText(PurchaseActivity.this,"购买失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(PurchaseActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };

//    UserinfoListener userinfoListener=new UserinfoListener() {
//        @Override
//        public void onResponse(UserinfoBean userinfoBean) {
//            tv_username.setText(userinfoBean.getUsername());
//            et_address.setText(userinfoBean.getAddress());
//        }
//
//        @Override
//        public void onFail(String mag) {
//            Toast.makeText(PurchaseActivity.this,mag,Toast.LENGTH_SHORT).show();
//        }
//    };



}